package main


import "log"
import "github.com/go-gl/gl"
import glfw "github.com/go-gl/glfw3"

func init() {
}

var window glfw.Window

func main() {
  glfw.SetErrorCallback(onError)

  if !glfw.Init() {
    return
  }
  defer glfw.Terminate()

  window, err := glfw.CreateWindow(640, 480, "Kane", nil, nil)
  if err != nil {
    return
  }
  defer window.Destroy()

  window.MakeContextCurrent()
  glfw.SwapInterval(1)

  for !window.ShouldClose() {

    var width int
    var height int
    width, height = window.GetFramebufferSize()
    var ratio = float64(width) / float64(height)
    gl.Viewport(0, 0, width, height)
    gl.Clear(gl.COLOR_BUFFER_BIT)
    gl.MatrixMode(gl.PROJECTION)
    gl.LoadIdentity()
    gl.Ortho(-ratio, ratio, -1.0, 1.0, 1.0, -1.0)
    gl.MatrixMode(gl.MODELVIEW)
    gl.LoadIdentity()
    gl.Rotatef(float32(glfw.GetTime()) * 50.0, 0.0, 0.0, 1.0)
    gl.Begin(gl.TRIANGLES)
    gl.Color3f(1.0, 0.0, 0.0)
    gl.Vertex3f(-0.6, -0.4, 0.0)
    gl.Color3f(0.0, 1.0, 0.0)
    gl.Vertex3f(0.6, -0.4, 0.0)
    gl.Color3f(0.0, 0.0, 1.0)
    gl.Vertex3f(0.0, 0.6, 0.0)
    gl.End();

    window.SwapBuffers()
    glfw.PollEvents()
  }
}

func onError(code glfw.ErrorCode, desc string) {
  log.Fatal("Error %v: %v\n", code, desc)
}

//func onResize(w int, h int) {
//}
//
//func onMouseButton(button int, state int) {
//}
//
//func onKey(key glfw.Key,
//           scancode int,
//           action glfw.Action,
//           mods glfw.ModifierKey) {
//  switch key {
//  case glfw.KeyEsc:
//    window.SetWindowShouldClose(true)
//  case 67: // 'c'
//    gl.Clear(gl.COLOR_BUFFER_BIT)
//  }
//}

